package net.hn.hnms.biz.meianshen.schedule;

import net.hn.hnms.biz.meianshen.domain.*;
import net.hn.hnms.biz.meianshen.respoistory.*;
import net.hn.hnms.biz.meianshen.service.KHDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2023/3/8 10:23
 * 1.0 version
 */
@Component
public class JobTask {
    @Autowired
    private KHDataService service;
    @Autowired
    private BeltRepository beltRepository;
    @Autowired
    private DrainRepository drainRepository;
    @Autowired
    private CompressedNitrogenRepository compressedNitrogenRepository;
    @Autowired
    private PowerRepository powerRepository;
    @Autowired
    private PointRepository pointRepository;
    @Autowired
    private MiningRepository miningRepository;
    @Autowired
    private FanRepository fanRepository;

    @Scheduled(cron = "0 */1 *  * * ? ")//主运输
    @Transactional
    public void belt() {
        try {

            List<String> strings = service.TestKDBTagGetNames("ST01*");
            List<Belt> baseEntities = service.TestKDBDataGetCurrentValue(strings);
            beltRepository.deleteAll();
            beltRepository.saveAll(baseEntities);
        } catch (Exception e) {
            if (e.getMessage().contains("Connection reset")) {
                service.TestKDBServerDisconnect();
                service.TestKDBServerConnect();
            }
        }
    }

    @Scheduled(cron = "15 */1 *  * * ? ")//排水
    @Transactional
    public void DrainWater() throws IllegalAccessException, InstantiationException, InvocationTargetException {
        List<String> strings = service.TestKDBTagGetNames("ST04*");
        List<DrainWater> baseEntities = service.drainWaterGetCurrentValue(strings);
        drainRepository.deleteAll();
        drainRepository.saveAll(baseEntities);
    }

    @Scheduled(cron = "25 */1 *  * * ? ")//压风制氮
    @Transactional
    public void CompressedNitrogen() {
        List<String> strings = service.TestKDBTagGetNames("ST05*");
        List<CompressedNitrogen> baseEntities = service.compressedNitrogenGetCurrentValue(strings);
        compressedNitrogenRepository.deleteAll();
        compressedNitrogenRepository.saveAll(baseEntities);
    }

    @Scheduled(cron = "35 */1 *  * * ? ")//供电
    @Transactional
    public void power() {
        List<String> strings = service.TestKDBTagGetNames("ST25*");//3-1煤
        List<Power> baseEntities = service.powerGetCurrentValue(strings);
        List<String> strings35 = service.TestKDBTagGetNames("ST06*");//35kv
        baseEntities.addAll(service.powerGetCurrentValue(strings35));
        powerRepository.deleteAll();
        powerRepository.saveAll(baseEntities);
    }

    @Scheduled(cron = "0 50 11  */1 * ? ")
    @Transactional
    public void PointDescription() {
        List<PointDescription> list = service.TestKDBTagGetNames();
        pointRepository.deleteAll();
        pointRepository.saveAll(list);

    }

    @Scheduled(cron = "45 */1 *  * * ? ")//采煤机
    @Transactional
    public void mining() {

        List<String> strings = service.TestKDBTagGetNames("ST26*");
        List<Mining> baseEntities = service.miningGetCurrentValue(strings);
        miningRepository.deleteAll();
        miningRepository.saveAll(baseEntities);
    }

    @Scheduled(cron = "30 */1 *  * * ? ")//通风
    @Transactional
    public void fan() {

        List<String> strings = service.TestKDBTagGetNames("ST24*");//白家渠
        strings.addAll(service.TestKDBTagGetNames("ST08*"));//赵家渠
        List<Fan> baseEntities = service.fanGetCurrentValue(strings);
        fanRepository.deleteAll();
        fanRepository.saveAll(baseEntities);
    }

    @Scheduled(cron = "10 0 */1  * * ? ")//重置连接
    @Transactional
    public void restClient() {
        service.TestKDBServerDisconnect();
        service.TestKDBServerConnect();
    }
}
